package users

import (
	"github.com/gin-gonic/gin"
	"log"
	"tao-admin/configs"
	"tao-admin/internal/model"
	"tao-admin/utils"
)

type UserManager struct{}

type SearchUserCondition struct {
	Id            *int64  `json:"id"`
	Username      *string `json:"username"`
	Name          *string `json:"name"`
	Password      *string `json:"password"`
	Email         *string `json:"email"`
	Telephone     *string `json:"telephone"`
	ImgUrl        *string `json:"img_url"`
	Role          *string `json:"role"`
	LastLoginTime *int64  `json:"lastLoginTime"`
	IsValid       *int64  `json:"isValid"`
	Ctime         *int64  `json:"ctime"`
	Mtime         *int64  `json:"mtime"`
}

func (manager *UserManager) SearchUserByConditionMng(ctx *gin.Context, condition *SearchUserCondition) ([]model.UserTab, int64, error) {
	db := configs.GetGormDB()
	list := make([]model.UserTab, 0)
	var total int64 = 0

	if condition.Username != nil {
		db = db.Where("username=?", condition.Username)
	}
	if condition.Name != nil {
		db = db.Where("name=?", condition.Name)
	}
	if condition.IsValid != nil {
		db = db.Where("isValid=?", condition.IsValid)
	}
	if condition.Email != nil {
		db = db.Where("email=?", condition.Email)
	}
	if condition.Telephone != nil {
		db = db.Where("telephone=?", condition.Telephone)
	}
	if condition.Role != nil {
		db = db.Where("role=?", condition.Role)
	}
	if condition.Ctime != nil {
		db = db.Where("ctime=?", condition.Ctime)
	}
	if condition.Mtime != nil {
		db = db.Where("mtime=?", condition.Mtime)
	}

	err1 := db.Model(&model.UserTab{}).Count(&total).Error
	//db = db.Order("priority DESC, status ASC")
	db = db.Order("id DESC")

	err2 := db.Find(&list).Error
	if err1 != nil || err2 != nil {
		return nil, 0, nil
	}

	log.Printf("查表结束, list: %v", list)
	return list, total, nil
}

func (manager *UserManager) CreateUserMng(ctx *gin.Context, item model.UserTab) error {
	db := configs.GetGormDB()
	err := db.Create(&item).Error
	if err != nil {
		return err
	}
	return nil
}

func (manager *UserManager) UpdateUserMng(ctx *gin.Context, item model.UserTab) error {
	db := configs.GetGormDB()
	// 先赋值
	updateData := model.UserTab{
		Username:      item.Username,
		Name:          item.Name,
		Password:      item.Password,
		Email:         item.Email,
		Telephone:     item.Telephone,
		ImgUrl:        item.ImgUrl,
		Role:          item.Role,
		LastLoginTime: item.LastLoginTime,
		IsValid:       item.IsValid,
		Ctime:         item.Ctime,
		Mtime:         item.Mtime,
	}

	id := item.Id
	// 查询记录
	if err := db.Where("id = ?", id).First(&item).Error; err != nil {
		return err
	}

	err := db.Model(&item).Where("id = ?", id).Update(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

func (manager *UserManager) UpdateLoginMng(item model.UserTab) error {
	db := configs.GetGormDB()
	// 先赋值
	timeStamp := utils.GetCurrentTimeStamp()
	updateData := model.UserTab{
		LastLoginTime: &timeStamp,
	}
	id := item.Id
	// 查询记录
	if err := db.Where("id = ?", id).First(&item).Error; err != nil {
		return err
	}
	err := db.Model(&item).Where("id = ?", id).Update(updateData).Error
	if err != nil {
		return err
	}
	return nil
}
